﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.ArrivalNotice
{
    public class ArrivalNoticesUpdateHandler : IRequestHandler<ArrivalNoticesUpdateCommand, ResultsDTO>
    {
        #region 仓储 
        private readonly IBateRepository<ArrivalNoticesDto> ArrivalNoticeRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ArrivalNoticeRepository"></param>
        public ArrivalNoticesUpdateHandler(IBateRepository<ArrivalNoticesDto> ArrivalNoticeRepository)
        {
            this.ArrivalNoticeRepository = ArrivalNoticeRepository;
        }
        #endregion

        #region 到货通知 修改
        /// <summary>
        /// 到货通知 修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO> Handle(ArrivalNoticesUpdateCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultsDTO对象result，并设置Code为 0
                ResultsDTO result = new ResultsDTO() { Code = 0 };
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper helper = new DapperHelper();

                ArrivalNoticesDto dto = new ArrivalNoticesDto();

                //连接mySql 
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                string str = "UPDATE arrivalnotice SET ArrivalNoticeNode='" + request.ArrivalNoticeNode + "',ArrivalNoticeName='" + request.ArrivalNoticeName + "',PurchaseIndentId='" + request.PurchaseIndentId + "',ArrivalNoticeTime='" + request.ArrivalNoticeTime + "',PhoneName='" + request.PhoneName + "',Phone='" + request.Phone + "',Remark='" + request.Remark + "',Documents='" + request.Documents + "',SupplierName='" + request.SupplierName + "' WHERE ArrivalNoticeId=" + request.ArrivalNoticeId + "";

                var getnum = ArrivalNoticeRepository.Add(str, dto, conn);
                if (getnum != null)
                {
                    result.Code = 1;
                }

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
